//
//  BusinessWhsWhsItemListViewController.m
//  HAODE
//
//  Created by panquanReabam.com on 16/1/19.
//  Copyright © 2016年 reabam.com. All rights reserved.
//

#import "BusinessWhsWhsItemListViewController.h"
#import "InputHelper.h"
#import "HAODEsingleton.h"
#import "UIViewExt.h"
#import "HttpRequest.h"
#import "WhsCell.h"

#import "BusinessWhsEditWhsViewController.h"
@interface BusinessWhsWhsItemListViewController ()<UISearchBarDelegate,UITextFieldDelegate>

@end

@implementation BusinessWhsWhsItemListViewController

{
    UISearchBar *search;
    HAODEsingleton* commdity;
    NSMutableArray*code;
    NSMutableArray*content;
    NSMutableArray*isDefault;
    
}





@synthesize alt = alt;


-(void)viewDidLoad{
    
    [super viewDidLoad];
}
-(void)BackAction:(UIButton *)sender{
    
    [self.navigationController popViewControllerAnimated:YES];
}
- (void)viewDidDisappear:(BOOL)animated

{
    
    [super viewDidDisappear:animated];
    
    
    
    self.navigationController.delegate = nil;
    
}



-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    self.xuanzhong=nil;
 
       UIImage* backImage = [UIImage imageNamed:@"fanhui"];
    CGRect backframe = CGRectMake(0,0,30,30);
    UIButton* backButton= [[UIButton alloc] initWithFrame:backframe];
    [backButton setBackgroundImage:backImage forState:UIControlStateNormal];
    backButton.titleLabel.font=[UIFont systemFontOfSize:13];
    [backButton addTarget:self action:@selector(BackAction:) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem* leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:backButton];
    self.navigationItem.leftBarButtonItem = leftBarButtonItem;
    
    self.title=@"选择仓库";
//    UIButton*tianjiaBtn = [[UIButton alloc] initWithFrame:CGRectMake(30, 0.0,  60/2, 60/2)];
//    [tianjiaBtn setImage:[UIImage imageNamed:@"tianjia"] forState:UIControlStateNormal];
//    [tianjiaBtn addTarget:self action:@selector(addProduct:) forControlEvents:UIControlEventTouchUpInside];
//    UIBarButtonItem* myBtnBtn = [[UIBarButtonItem alloc] initWithCustomView:tianjiaBtn];
//    self.navigationItem.rightBarButtonItem = myBtnBtn;
    
    [self.navigationController.navigationBar setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:
                                                                     [UIColor colorWithRed:0/255.0 green:0/255.0 blue:0/255.0 alpha:1.0],
                                                                     UITextAttributeTextColor,
                                                                     [UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:1],
                                                                     UITextAttributeTextShadowColor,
                                                                     [NSValue valueWithUIOffset:UIOffsetMake(0, 0)],
                                                                     UITextAttributeTextShadowOffset,
                                                                     [UIFont fontWithName:@"Helvetica Neue" size:tetlefot],
                                                                     UITextAttributeFont,nil]];
    
    _table.delegate=self;
    _table.dataSource=self;
    
    _table.frame=CGRectMake(0,0, ScreenWidth, ScreenHeight-63);
    commdity=[HAODEsingleton shared];
    _table.backgroundColor= [UIColor colorWithRed:255/255.0 green:255/255.0 blue:255/255.0 alpha:1.0];
    [self add];
    _table.separatorStyle=UITableViewCellSeparatorStyleNone;
//     [inputHelper setupInputHelperForView:_table withDismissType:InputHelperDismissTypeTapGusture];
   
}

-(void)add{
    code=[[NSMutableArray alloc]init];
    content=[[NSMutableArray alloc]init];
    isDefault=[[NSMutableArray alloc]init];
    
    
    
        NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
        NSString* Token = [user objectForKey:@"tokenId"];
        NSMutableDictionary *param1 = [NSMutableDictionary new];
        [param1 setValue:Token forKey:@"tokenId"];
    
    
    
        [HttpRequest requestData:@"Business/Whs/WhsItemList" params:param1 success:^(BOOL isSuccess, SeverData *severDataDict) {
            if (isSuccess) {
                if ([[severDataDict.dataDict objectForKey:@"ResultInt"] integerValue]==0) {
                    [Global hideProgressHUDInternal:self.view];
    
    
                    NSLog(@"aaaaaaaa%@",severDataDict.dataDict);
    
                    NSMutableArray* arrr=[[NSMutableArray alloc]init];
                    arrr=[severDataDict.dataDict objectForKey:@"DataLine"];
                    for (NSDictionary* dicc in arrr )
                    {
                        [content addObject:[dicc valueForKey:@"whsName"]];
    
                        [isDefault addObject:[dicc valueForKey:@"isDefault"]];
    
                        [code addObject:[dicc valueForKey:@"id"]];
    
                    }
    
                    [_table reloadData];
    
                }else{
                    [Global hideProgressHUDInternal:self.view];
                    NSString *requestResultStr=[severDataDict.dataDict objectForKey:@"ResultString"];
                    [Global showTextHudSub:requestResultStr view:self.view];
                }
    
            }
            else
            {
                [Global hideProgressHUDInternal:self.view];
                NSLog(@"aaaaaaaa%@",severDataDict.dataDict);
                [Global showTextHudSub:@"连接失败" view:self.view];
            }
            
        }];
    
    
    
    
}


//-(void)addProduct:(UIButton*)sender{
//
//        BusinessWhsEditWhsViewController *detailViewController = [[BusinessWhsEditWhsViewController alloc] init];
//        detailViewController.titleName=@"添加仓库";
//        detailViewController.sat=@"A";
//        [self.navigationController pushViewController:detailViewController animated:YES];
// 
//    
//}
//

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [content count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    
    WhsCell* cell = (WhsCell*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil)
    {
        NSArray* nib = [[NSBundle mainBundle] loadNibNamed:@"WhsCell" owner:self options:nil];
        cell = [nib objectAtIndex:0];
    }
    
    
    
    cell.code=[code objectAtIndex:indexPath.row];
    cell.textfield .frame=CGRectMake(15,0,ScreenWidth-70,55);
    [cell.textfield setBorderStyle:   UITextBorderStyleNone]; //外框类型
    //      searchTextField.placeholder = @"  会员/手机号/姓名"; //默认显示的字
    cell.textfield.font = [UIFont systemFontOfSize: 14];
    cell.textfield.tag=indexPath.row;
    cell.textfield.returnKeyType = UIReturnKeyDone;
    cell.textfield.clearButtonMode = UITextFieldViewModeWhileEditing; //编辑时会出现个修改X
    cell.textfield.delegate = self;
    cell.textfield.tintColor =tintCol;
    cell.textfield.text = [content objectAtIndex:indexPath.row] ;
    cell.textfield.textColor=[UIColor blackColor];
    cell.textfield.userInteractionEnabled=NO;
    cell.bianji.frame=CGRectMake(ScreenWidth-40, 0,30, 55);
    cell.bianji.tag=indexPath.row;
    [cell.bianji setTitle:@"  " forState:UIControlStateNormal];
    [cell.bianji setTitleColor:shenhuicolor forState:UIControlStateNormal];
    cell.bianji.titleLabel.font=[UIFont systemFontOfSize:conten14fot];
    
    cell.saveAccount=YES;
    
//    [cell.bianji addTarget:self action:@selector(bianji:) forControlEvents:UIControlEventTouchUpInside];
    
    
    
    
    
    if ([[isDefault objectAtIndex:indexPath.row]isEqualToString:@"Y"]) {
        NSString *s12 = [NSString stringWithFormat:@"%@", [content objectAtIndex:indexPath.row]];
        UIFont *font12 = [UIFont systemFontOfSize:contenfot];
        CGSize size12 = CGSizeMake(320, 2000);
        CGSize labelsize12 = [s12 sizeWithFont:font12 constrainedToSize:size12 lineBreakMode:NSLineBreakByCharWrapping];
        UILabel*isDefaults=[[UILabel alloc]initWithFrame:CGRectMake(labelsize12.width+30, 20,35, 16)];
        isDefaults.text=@"默认";
        isDefaults.backgroundColor=luseColor;
        isDefaults.textAlignment=NSTextAlignmentCenter;
        isDefaults.textColor=[UIColor whiteColor];
        isDefaults.font=[UIFont systemFontOfSize:11];
        [cell.contentView addSubview:isDefaults];
        cell.textfield.userInteractionEnabled=NO;
      
    }else{

    }
    
    
    
    if (content.count !=indexPath.row+1) {
        UILabel *lineLabel1=[[UILabel alloc] initWithFrame:CGRectMake(15, 55-0.35, ScreenWidth-30, 0.35)];
        lineLabel1.backgroundColor=qianhuicolor;
        [cell.contentView addSubview:lineLabel1];
        
    }
    
    if (content.count ==indexPath.row+1) {
        UILabel *lineLabel1=[[UILabel alloc] initWithFrame:CGRectMake(0, 55-0.35, ScreenWidth, 0.35)];
        lineLabel1.backgroundColor=qianhuicolor;
        [cell.contentView addSubview:lineLabel1];
        
    }
    
    
    //    }
    return cell;
}

- (CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 55;
}





-(void)bianji:(UIButton *)sender{
    
//    
//    BusinessWhsEditWhsViewController *detailViewController = [[BusinessWhsEditWhsViewController alloc] init];
//    detailViewController.titleName=@"修改仓库";
//    detailViewController.sat=@"U";
//     commdity.whsId=[code objectAtIndex:sender.tag];
//    commdity.whsName=[content objectAtIndex:sender.tag];
//    [self.navigationController pushViewController:detailViewController animated:YES];
//   
    
}
#pragma mark - Table view delegate

//
//-(UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{
//    return  UITableViewCellEditingStyleDelete;
//    
//}
//- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
//    return YES;
//}
//
//
//-(NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath
//
//{
//    
//    return @"删除";
//    
//}
//- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
//{
//    self.codestring=[code objectAtIndex:indexPath.row];
//    
//    NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
//    NSString* Token = [user objectForKey:@"tokenId"];
//    NSMutableDictionary *params = [NSMutableDictionary new];
//    
//    [params setValue:Token forKey:@"tokenId"];
//    [params setValue:@"D" forKey:@"editType"];
//    [params setValue:self.codestring forKey:@"id"];
//    [Global showIndeterminateHudSub:@"加载中，请稍后。。。" delay:30 view:self.view];
//    
//    
//    [HttpRequest requestData:@"Business/Whs/EditWhs" params:params success:^(BOOL isSuccess, SeverData *severDataDict) {
//        if (isSuccess) {
//            
//            
//            NSLog(@"33333333====%@",severDataDict.dataDict);
//            if ([[severDataDict.dataDict objectForKey:@"ResultInt"] integerValue]==0) {
//                [Global hideProgressHUDInternal:self.view];
//                [self add];
//                
//                
//                
//            }else{
//                [Global hideProgressHUDInternal:self.view];
//                NSString *requestResultStr=[severDataDict.dataDict objectForKey:@"ResultString"];
//                [Global showTextHudSub:requestResultStr view:self.view];
//                [self add];
//            }
//            
//        }
//        else
//        {
//            [Global hideProgressHUDInternal:self.view];
//            [self add];
//            [Global showTextHudSub:@"连接失败" view:self.view];
//        }
//        
//    }];
//
//    
//    
//}
//
//

#pragma mark - Table view delegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
  
            commdity.whsId=code[indexPath.row];
            commdity.whsName=content[indexPath.row];
            [self.navigationController popViewControllerAnimated:YES];
   
    
}
- (void)showAlertWait
{
    alt = [[UIAlertView alloc] initWithTitle:@"网络不稳定 ！" message:@"请确认网络" delegate:self cancelButtonTitle:nil otherButtonTitles: nil];
    [alt show];
    
    [self performSelector:@selector(hideAlert) withObject:nil afterDelay:1.0f];
    
}

- (void)showAlertMessage:(NSString *)msg
{
    alt = [[UIAlertView alloc] initWithTitle:@"温馨提示" message:msg delegate:nil cancelButtonTitle:nil otherButtonTitles: nil];
    [alt show];
    
    [self performSelector:@selector(hideAlert) withObject:nil afterDelay:1.0f];
}
- (void)hideAlert
{
    if (alt != nil)
    {
        [alt dismissWithClickedButtonIndex:0 animated:YES];
        alt = nil;
    }
}
@end